﻿using System;
using System.Web;
using System.ServiceModel.Activation;
using System.ServiceModel;

namespace GroupChallenge.Web
{
    [ServiceKnownType(typeof(QuestionType))]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class GroupChallenge : IGroupChallenge
    {
        // Current single game in play in the Application.
        private GameManager Context
        {
            get
            {
                GameManager ret = HttpContext.Current.Application["CurrentGameManager"] as GameManager;

                if (null == ret)
                {
                    ret = new GameManager();

                    // Should be in the application cache.
                    HttpContext.Current.Application["CurrentGameManager"] = ret;
                }

                return ret;
            }
        }

        // Join a player to the current game.
        public FriendlyResult JoinGame(string nickName, string fullName, int playerId)
        {
            return Context.JoinGame(nickName, fullName, playerId);
        }

        // Remove the player from the current game.
        public bool LeaveGame(string nickName)
        {
            return Context.LeaveGame(nickName);
        }

        // Get a nickname suggestion list.
        public string[] GetNickNameSuggestions()
        {
            return Context.GetNickNameSuggestions();
        }

        // Get the current game state for client updates.
        public void GetCurrentGameState(ref DateTime lastChangeTime, out Game currentGame, out Question currentQuestion)
        {
            Context.GetCurrentGameState(ref lastChangeTime, out currentGame, out currentQuestion);
        }

        // Player answering a question.  Provide indication if the question is correct and if they are the first player to get it correct.
        public void AnswerQuestion(string userNickName, int questionId, int answerId, out int correctAnswerId, out bool isFirstToAnswer)
        {
            Context.AnswerQuestion(userNickName, questionId, answerId, out correctAnswerId, out isFirstToAnswer);
        }

        // Submit a question from a player and return a friendly response.
        public FriendlyResult SubmitQuestion(string userNickName, Question newQuestion)
        {
            return Context.SubmitQuestion(userNickName, newQuestion);
        }
    }
}
